﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Wicresoft.Framework.Organization;
using Wicresoft.Busi.BizLogic;
using Wicresoft.Busi.Entity;
using System.Text.RegularExpressions;

public partial class Car_VehicleInfoDetail : BaseAuthenticationPage
{
    #region Property VehicleID
    public int VehicleID
    {
        get
        {
            if (Request["VehicleID"] != null)
                return int.Parse(Request["VehicleID"].ToString());
            else
                return int.MinValue;
        }

    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        this.UseTabber = true;
        if (!IsPostBack)
        {
            initVehicleType();
            initVehicleStatus();
            this.tdSave.Visible = true;

            if (VehicleID != int.MinValue)
            {
                LoadVehicleInfo();
                this.fg1.Visible = false;
                this.tdReset.Visible = false;
                this.fg2.Visible = true;
                this.tdDelete.Visible = true;
            }
            else
            {
                this.imgVehicle.ImageUrl = "../Images/NoPreview.jpg";
                this.fg1.Visible = true;
                this.tdReset.Visible = true;
                this.fg2.Visible = false;
                this.tdDelete.Visible = false;
            }
        }
    }


    #region 初始化车辆类型下拉框
    private void initVehicleType()
    {
        DataTable dt = BLDic_Vehicle.GetVehicleType();
        this.ddlVehicleType.DataSource = dt;
        this.ddlVehicleType.DataTextField = Dic_Vehicle.SQLCOL_PROPERTYNAME;
        this.ddlVehicleType.DataValueField = Dic_Vehicle.SQLCOL_PROPERTYID;
        this.ddlVehicleType.DataBind();
    }
    #endregion

    #region 初始化车辆使用状态下拉框
    private void initVehicleStatus()
    {
        DataTable dt = BLDic_Vehicle.GetVehicleStatus();
        this.radLstAvailable.DataSource = dt;
        this.radLstAvailable.DataTextField = Dic_Vehicle.SQLCOL_PROPERTYNAME;
        this.radLstAvailable.DataValueField = Dic_Vehicle.SQLCOL_PROPERTYID;
        this.radLstAvailable.DataBind();
        if (this.radLstAvailable.Items.Count > 0)
            this.radLstAvailable.Items[0].Selected = true;
    }
    #endregion

    #region 获取车辆信息
    private void LoadVehicleInfo()
    {
        Vehicles info = new Vehicles(this.VehicleID);
        info.Load();

        if (info.Picture != string.Empty)
            this.imgVehicle.ImageUrl = "../images/Vehicles/" + info.Picture;
        else
            this.imgVehicle.ImageUrl = "../images/NoPreview.jpg";

        this.imgVehicle.Visible = true;
        if (info.VehicleType != int.MinValue)
        {
            ListItem li = ddlVehicleType.Items.FindByValue(info.VehicleType.ToString());
            if (li != null)
            {
                this.ddlVehicleType.SelectedValue = li.Value;
            }
        }
        this.txtVehicleNo.Text = info.VehicleNo;
        this.txtVehicleName.Text = info.VehicleName;
        this.txtSeatsCount.Text = info.SeatsCount.ToString();
        this.txtDescription.Text = info.Description;
        this.radLstAvailable.SelectedValue = info.Flag;
        this.tbDriverPhone.Text = info.DriverPhone;

    }
    #endregion

    protected void btnSave_Click(object sender, System.EventArgs e)
    {
        int iSeatsCount;
        if (this.txtVehicleNo.Text == string.Empty)
        {
            WebCommon.ShowMessage(this.Page, "车牌号码不能为空.");
            this.txtVehicleNo.Focus();
            return;
        }
        if (this.txtVehicleName.Text == string.Empty)
        {
            WebCommon.ShowMessage(this.Page, "车型不能为空.");
            txtVehicleName.Focus();
            return;
        }
        if (this.tbDriverPhone.Text.Trim() != string.Empty && !Regex.IsMatch(this.tbDriverPhone.Text.Trim(),"^1\\d{10}$"))
        {
            WebCommon.ShowMessage(this.Page, "司机手机有误.");
            tbDriverPhone.Focus();
            return;
        }
        try
        {
            iSeatsCount = int.Parse(this.txtSeatsCount.Text);
        }
        catch
        {
            iSeatsCount = 1;
        }

        Vehicles info = new Vehicles();
        if (this.VehicleID == int.MinValue)
        {
            try
            {
                //检查上传文件不为空
                if (this.flVehiclePic.PostedFile.FileName != "")
                {
                    info.Picture = SaveFile();
                }
            }
            catch
            {
                WebCommon.ShowMessage(this.Page, "上传图片失败.");
                return;
            }

            info.VehicleNo = this.txtVehicleNo.Text;
            info.VehicleType = int.Parse(this.ddlVehicleType.SelectedValue);
            info.VehicleName = this.txtVehicleName.Text;
            info.SeatsCount = iSeatsCount;
            info.GroupID = BasePage.GetDept(CurrentUser.ID).ID;
            info.GroupName = BasePage.GetDept(CurrentUser.ID).DisplayName;
            info.Conservator = CurrentUser.Name;
            info.ConservatorName = CurrentUser.DisplayName;
            info.Description = this.txtDescription.Text;
            info.Flag = this.radLstAvailable.SelectedValue;
            info.DriverPhone = this.tbDriverPhone.Text.Trim();
            info.Add();
        }
        else
        {
            info.ID = this.VehicleID;
            info.Load();
            try
            {
                //检查上传文件不为空
                if (this.flVehiclePic.PostedFile.FileName != "")
                {
                    if (System.IO.File.Exists(Request.PhysicalApplicationPath + "\\images\\Vehicles\\" + info.Picture))
                        System.IO.File.Delete(Request.PhysicalApplicationPath + "\\images\\Vehicles\\" + info.Picture);

                    info.Picture = SaveFile();
                }
            }
            catch
            {
                WebCommon.ShowMessage(this.Page, "上传图片失败.");
                return;
            }

            info.VehicleType = int.Parse(this.ddlVehicleType.SelectedValue);
            info.VehicleNo = this.txtVehicleNo.Text;
            info.VehicleName = this.txtVehicleName.Text;
            info.SeatsCount = iSeatsCount;
            info.GroupID = BasePage.GetDept(CurrentUser.ID).ID;
            info.GroupName = BasePage.GetDept(CurrentUser.ID).DisplayName;
            info.Conservator = CurrentUser.Name;
            info.ConservatorName = CurrentUser.DisplayName;
            info.Description = this.txtDescription.Text;
            info.Flag = this.radLstAvailable.SelectedValue;
            info.DriverPhone = this.tbDriverPhone.Text.Trim();
            info.UpdateByPrimaryKey();
        }

        ClientScript.RegisterStartupScript(this.GetType(), "RefreshParent", "<script>alert('保存成功');parent.document.getElementById('ctl00$ListToolBarPlaceHolder$btnRefresh').click();</script>");
        //WebCommon.ShowMessage(this.Page, "操作成功.");
    }

    private bool JudgeExtendName(string extendName)
    {
        if (!extendName.ToLower().Equals(".jpg") && !extendName.ToLower().Equals(".bmp") && !extendName.ToLower().Equals(".gif"))
        {
            WebCommon.ShowMessage(this.Page, "文件格式有误，请上传jpg、bmp或者gif文件");
            return false;
        }
        else
            return true;
    }

    protected void btnDelete_Click(object sender, System.EventArgs e)
    {
        Vehicles info = new Vehicles(this.VehicleID);
        info.Delete();
        ClientScript.RegisterStartupScript(this.GetType(), "RefreshParent", "<script>alert('删除成功');parent.document.getElementById('ctl00$ListToolBarPlaceHolder$btnRefresh').click();</script>");
    }

    private string SaveFile()
    {
        string fileName;
        string extendName;
        string originalPath = this.flVehiclePic.PostedFile.FileName;
        int i = originalPath.LastIndexOf(".");
        int j = originalPath.LastIndexOf("\\");
        extendName = originalPath.Substring(i);

        if (!JudgeExtendName(extendName))
        {
            return string.Empty;
        }

        fileName = Guid.NewGuid().ToString() + extendName;

        this.flVehiclePic.PostedFile.SaveAs(Request.PhysicalApplicationPath + "\\images\\Vehicles\\" + fileName);
        return fileName;
    }

    protected void btnReset_Click(object sender, System.EventArgs e)
    {
        if (this.VehicleID == int.MinValue)
        {
            this.imgVehicle.Visible = false;
            this.ddlVehicleType.SelectedIndex = 0;
            this.txtVehicleName.Text = string.Empty;
            this.txtSeatsCount.Text = "0";
            this.txtDescription.Text = string.Empty;
            this.tbDriverPhone.Text = string.Empty;
            this.radLstAvailable.SelectedIndex = 0;
        }
        else
        {
            LoadVehicleInfo();
        }
    }
}
